import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

///放大缩小动画
class ScalAlpaAnimation extends StatefulWidget {
  final String? text;

  ScalAlpaAnimation({this.text}) : super();

  @override
  State<StatefulWidget> createState() {
    return ScaleAlpaState();
  }
}

class ScaleAlpaState extends State<ScalAlpaAnimation>
    with TickerProviderStateMixin {
  AnimationController? _controller;
  List list = [
    "assets/images/iron_man.jpg",
    "assets/images/women_one.jpg",
    "assets/images/women_two.jpg"
  ];

  Animation<double>? sizeAnimation;
  Animation<double>? scalAnimation;
  String? iamge;

  @override
  void initState() {
    super.initState();
    _controller =
        AnimationController(vsync: this, duration: Duration(seconds: 1));
    sizeAnimation = Tween<double>(begin: 0.5, end: 2).animate(
        CurvedAnimation(parent: _controller!, curve: Interval(0.1, 1.0)));
    scalAnimation = Tween<double>(begin: 1, end: 0).animate(
        CurvedAnimation(parent: _controller!, curve: Interval(0.5, 1.0)));
    _controller?.forward();
    _controller?.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        print(
            "status is  $status and  index is ${list.first}  length is ${list.length})");
        if (list.length != 1) {
          list.remove(list.first);
          setState(() {
            print("status is------");
            _controller?.reset(); //重置动画
            // index++;
          });
        }
      } else if (status == AnimationStatus.dismissed) {
        print("status is dismissed");
        //开始动画
        _controller?.forward();
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.text ?? ""),
      ),
      body: Center(
        child: FadeTransition(
          opacity: scalAnimation!,
          child: ScaleTransition(
            scale: sizeAnimation!,
            child: ClipOval(
              child: Image.asset(
                list.first,
                height: 100,
                width: 100,
                fit: BoxFit.cover,
              ),
            ),
          ),
        ),
      ),
    );
  }

  @override
  void dispose() {
    _controller?.dispose();
    super.dispose();
  }
}
